const express = require('express');
const Router = express.Router();
const utility = require('utility');


const mongoose = require('mongoose');
require('./model_user.js');
const User = mongoose.model("userSchema");

// 路由拦截Api
Router.get('/info',(req,res)=>{
    //juge coolie
    const COOKIEID = req.cookies.COOKIEID;
    if(!COOKIEID){
        return res.json({
            code:1,
            data:{}
        })
    }else{
        User.findOne({_id:COOKIEID},{password:0},(err,user) => {
            if(err){
                return res.json({
                    code:-1,
                    msg:'服务有问题'
                })
            }else{
                return res.json({
                    code:0,
                    data:user
                })
            }
        })
    }
})
// 注册
Router.post('/regist',(req,res)=>{
    const {name,password,repeatpassword,type} = req.body;
    User.findOne({name}).then((user) => {
        if(user){
            return res.json({
                code:-2,
                msg:'用户名已经存在'
            })
        }else{
            if(password !== repeatpassword){
                return res.json({
                    code:-2,
                    data:'两次密码不一致，请重新输入'
                })
            }
            new User({name,password:md5Pwd(password),type}).save().then((rel) => {
                res.cookie('COOKIEID',rel._id);
                const {name,type,_id} = rel;
                return res.json({
                    code:0,
                    data:{name,type,_id},
                    mag:'用户注册成功'
                })
            })
        }
    })
})

// 加密
function md5Pwd(pwd){
    // 加盐
    let str = 'jjkdfvvjdfnmnksakdkl145555kosjja144ds@#%';
    pwd += str;
    return utility.md5(utility.md5(pwd));
}

// 登陆
Router.post('/login',(req,res)=>{
    const {name,password} = req.body;
    // 第一个查询条件，第二个显示条件
    User.findOne({name,password:md5Pwd(password)},{password:0}).then((user) => {
        if(user){
            // 登陆成功，设置cookie
            res.cookie('COOKIEID',user._id);
            return res.json({
                code:0,
                data:user,
                msg:'登陆成功'
            })
        }else{
            return res.json({
                code:-1,
                msg:'用户名或密码错误'
            })
        }
    })
})

// 获取当前用户
Router.get('/current',(req,res)=>{
    User.find({name: req.query.name},(err,user)=>{
        console.log(0)
        console.log(user)
        if(err){
            res.json({
                code:1,
                msg:"获取用户失败"
            })
        }
        return res.json({
            code:0,
            data:user[0],
            msg:'获取成功'
        })
    })
})

// 获取所有
Router.get('/',(req,res)=>{
    User.find({}).then((data) => {
        return res.json({
            code:0,
            data:data
        })
    })
})

// 清空
Router.get('/remove',(req,res)=>{
    User.remove({}).then((data) => {
        return res.json({
            code:0,
            data:data,
            msg:'数据清除成功'
        })
    })
})

Router.get('/save',(req,res)=>{
    let newuser = {
        name:'name',
        password:'123',
        repeatpassword:'123',
        type:'boss',
        avatar:'',
        des:'我是老板',
        title:'过来',
        company:'阿里巴巴',
        paymoney:'1w+',
    }
    new User(newuser).save().then((data)=>{
        return res.json({
            code:0,
            data:data
        })
    })
})

/**
 * 注册完善信息
 */
Router.post('/perfect',(req,res)=>{
    if(!req.cookies.COOKIEID){
        res.json({
            code:-1,
            msg:'请重新登录'
        })
    }
    // req.cookies.COOKIEID 相当于 id
    User.findByIdAndUpdate(req.cookies.COOKIEID,req.body,(err,rel)=>{
        if(err){
            console.log(err)
        }else{
            const data = Object.assign({},{
                type:rel.type||'',
                name:rel.name||'',
                _id:rel._id||''
            },
            req.body)
            res.json({
                code:0,
                data:data,
                msg:'信息完善成功'
            })
        }
    })
})

/**
 * 获取求职者列表
 */
Router.get('/userlist',(req,res)=>{
    const type = req.query.type;
    User.find({type},(err,users)=>{
        return res.json({
            code:0,
            data:users,
            msg:'获取成功'
        })
    })
})

module.exports = Router;